package blancosMikel.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import blancosMikel.controller.beans.directorioBeans;

public class directorioDAO extends SigaabmDAO {

	public ArrayList<directorioBeans> loadPhones() {
		StringBuilder query = new StringBuilder();
		ArrayList<directorioBeans> telefonos = new ArrayList<directorioBeans>();
		directorioBeans dir;
		try {
			query.append("Select id_directorio,numero from directorio");
			conn = this.getConnectionMYSQL();
			ps = conn.prepareStatement(query.toString());
			rs = ps.executeQuery();
			System.out
					.println("dao.informativa.loadPhones.Ejecutado----------> "
							+ query.toString());

			while (rs.next()) {
				dir = new directorioBeans();
				dir.setId_directorio(rs.getInt(1));
				dir.setNumero(rs.getString(2));
				telefonos.add(dir);
			}
			System.out
					.println("dao.informativa.loadPhones----------> DirectorioCargado!");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close(conn);
			close(ps);
			close(rs);
		}
		return telefonos;
	}

	public int nextId() {
		int id = 0;
		try {
			StringBuilder query = new StringBuilder();
			query.append("Select id_directorio from directorio");
			conn = this.getConnectionMYSQL();
			ps = conn.prepareStatement(query.toString());
			rs = ps.executeQuery();
			System.out.println("dao.directorio.nextId.Ejecutado----------> "
					+ query.toString());

			while (rs.next()) {
				if (id < rs.getInt(1)) {
					id = rs.getInt(1);
				}
			}
			System.out.println("dao.directorio.nextId----------> Cargado");
		} catch (Exception e) {
			System.out.println("directorioDao...Error..." + e.getMessage());
		} finally {
			close(conn);
			close(ps);
			close(rs);
		}
		return id + 1;
	}

	public void insertDirectorio(directorioBeans bean) throws Exception {
		try {
			StringBuilder query = new StringBuilder();
			conn = this.getConnectionMYSQL();

			query.append("INSERT INTO directorio ")
					.append("(id_directorio,id_info,numero) ")
					.append("values (").append(bean.getId_directorio()).append(",").append(1)
					.append(",").append(bean.getNumero()).append(")");

			st = conn.createStatement();
			st.executeUpdate(query.toString());
			System.out
					.println("dao.directorio.insertdirectorio.Ejecutado----------> "
							+ query.toString());
			System.out
					.println("dao.directorio.insertdirectorio----------> insertado");

		} catch (SQLException error) {
			throw new Exception(error.getMessage());
		} catch (Exception error) {
			throw new Exception(error.getMessage());
		} finally {
			close(conn);
			close(st);
		}
	}

	public void deleteDirectorio(directorioBeans bean) throws Exception {
		try {
			StringBuilder query = new StringBuilder();
			conn = this.getConnectionMYSQL();

			query.append("DELETE FROM directorio where id_directorio=").append(
					bean.getId_directorio());

			st = conn.createStatement();
			st.executeUpdate(query.toString());
			System.out
					.println("dao.directorio.deletedirectorio.Ejecutado----------> "
							+ query.toString());
			System.out
					.println("dao.directorio.deleteDirectorio----------> eliminado");

		} catch (SQLException error) {
			throw new Exception(error.getMessage());
		} catch (Exception error) {
			throw new Exception(error.getMessage());
		} finally {
			close(conn);
			close(st);
		}
	}
}
